package fr.kotton.myorganizer.utils.sql;

import android.database.Cursor;

import java.util.Date;
import java.util.LinkedList;

import fr.kotton.myorganizer.bean.Task;
import fr.kotton.myorganizer.bean.TaskGroup;
import fr.kotton.myorganizer.db.MyOrganizerDbContract.TaskEntry;
import fr.kotton.myorganizer.db.MyOrganizerDbContract.TaskGroupEntry;
import fr.kotton.myorganizer.db.MyOrganizerDbContract.TaskToTaskGroupEntry;

public class DbToBeanConverter {

    public static LinkedList<Task> DbToTasks(Cursor pCursor) {

        LinkedList<Task> lTasksList = new LinkedList<Task>();

        if (pCursor.getCount() != 0) {

            pCursor.moveToFirst();

            do {
                Task lTask = new Task();

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_ID))) {
                    lTask.setId(pCursor.getInt(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_ID)));
                }

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_TITLE))) {
                    lTask.setTitle(pCursor.getString(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_TITLE)));
                }

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_ENDDATE))) {
                    lTask.setEndDate(new Date(pCursor.getLong(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_ENDDATE))));
                }

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_FREQUENCYUNIT))) {

                    lTask.setFrequencyUnit(pCursor.getString(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_FREQUENCYUNIT)));
                }

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_REMINDERDELAY))) {
                    lTask.setReminderDelay(new Date(pCursor.getLong(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_REMINDERDELAY))));
                }

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_CONTENTTYPE))) {
                    lTask.setTaskContentType(pCursor.getString(pCursor.getColumnIndex(TaskEntry.COLUMN_NAME_TASK_CONTENTTYPE)));
                }

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskToTaskGroupEntry.COLUMN_NAME_TASKTOTASKGROUP_TASKGROUP_ID))) {
                    lTask.addTaskGroupsId(pCursor.getInt(pCursor.getColumnIndex(TaskToTaskGroupEntry.COLUMN_NAME_TASKTOTASKGROUP_TASKGROUP_ID)));
                }

                lTasksList.add(lTask);
            } while (pCursor.moveToNext());
        }

        return lTasksList;
    }

    public static LinkedList<TaskGroup> DbToTaskGroups(Cursor pCursor) {
        LinkedList<TaskGroup> lTaskGroupsList = new LinkedList<TaskGroup>();

        if (pCursor.getCount() != 0) {

            pCursor.moveToFirst();

            do {
                TaskGroup lTaskGroup = new TaskGroup();

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskGroupEntry.COLUMN_NAME_TASKGROUP_ID))) {
                    lTaskGroup.setId(pCursor.getInt(pCursor.getColumnIndex(TaskGroupEntry.COLUMN_NAME_TASKGROUP_ID)));
                }

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskGroupEntry.COLUMN_NAME_TASKGROUP_NAME))) {
                    lTaskGroup.setName(pCursor.getString(pCursor.getColumnIndex(TaskGroupEntry.COLUMN_NAME_TASKGROUP_NAME)));
                }

                if (!pCursor.isNull(pCursor.getColumnIndex(TaskGroupEntry.COLUMN_NAME_TASKGROUP_COLOR))) {
                    lTaskGroup.setColor(pCursor.getString(pCursor.getColumnIndex(TaskGroupEntry.COLUMN_NAME_TASKGROUP_COLOR)));
                }

                lTaskGroupsList.add(lTaskGroup);
            } while (pCursor.moveToNext());
        }

        return lTaskGroupsList;
    }
}
